/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.ChuyenTauDTO;
import DTO.TuyenTauDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Nobita Chau
 */
public class TuyenTauDAO {
    public static boolean Them(TuyenTauDTO n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO `quanlyduongsat`.`tuyen` (`id`, `ghi_chu`,`loai_tuyen`, `ngay_tao`) "
                    + "VALUES ('%s', '%s','%d', '%s')", n.Id, n.GhiChu, n.LoaiTuyen, n.NgayTao);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }

    public static boolean Xoa(String id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from tuyen where id = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setString(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select * from tuyen";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(String MaTuyen)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From tuyen Where id like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, MaTuyen);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
       public static boolean Update(TuyenTauDTO C, String id)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "UPDATE `quanlyduongsat`.`tuyen` SET `ghi_chu`=?,`loai_tuyen`=?,`ngay_tao`=? WHERE `id`=? ";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, C.GhiChu);
            ps.setInt(2, C.LoaiTuyen);
            ps.setString(3,C.NgayTao);
      
            ps.setString(4,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
}
